package com.hsgene.product.controller;

import com.hsgene.common.util.qiniu.QiniuCertificateUtil;
import com.hsgene.common.util.resolver.IgnoreUserToken;
import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.support.OrderHelper;
import com.hsgene.product.condition.BannerCondition;
import com.hsgene.product.domain.GeneticBanners;
import com.hsgene.product.exception.ProductErrorCode;
import com.hsgene.product.service.BannerService;
import com.hsgene.product.service.GeneticBannersService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @desc:广告管理
 * @author: fuxiang
 * @date: 2018/10/12 11:26
 */
@RestController
@RequestMapping("/v1.0/genetic/testing/banners")
public class BannerController {

    @Autowired
    private GeneticBannersService geneticBannersService;

    @Autowired
    BannerService bannerService;

    /**
     * 添加广告图
     *
     * @param geneticBanners
     * @return
     */
    @PostMapping
    public ApiResult addBanner(@RequestBody GeneticBanners geneticBanners) {
        return bannerService.addBanner(geneticBanners);
    }

    /**
     * 修改广告图
     *
     * @param geneticBanners
     * @return
     */
    @PutMapping
    public ApiResult updateBanner(GeneticBanners geneticBanners) {
        return bannerService.updateBanner(geneticBanners);
    }

    /**
     * 列表(没有分页)
     *
     * @param
     * @return
     */
    @IgnoreUserToken(platform = 4)
    @GetMapping
    public ApiResult getPage(BannerCondition condition) {
        String platform = condition.getPlatform();
        if (StringUtils.isBlank(platform)) {
            return ApiResult.fail(ProductErrorCode.PLATFORM_IS_NULL);
        }
        if ("4".equals(condition.getPlatform())) {
            List<GeneticBanners> list = geneticBannersService.getBanner();
            if (list == null || list.size() < 0) {
                return ApiResult.succ(new ArrayList<>());
            }
            list.sort((GeneticBanners o1, GeneticBanners o2) -> o2.getSort() - o1.getSort());
            for (GeneticBanners banner : list) {
                String oldUrl = banner.getImageUrl();
                String newUrl = QiniuCertificateUtil.getDownloadCertificateStr(oldUrl);
                banner.setImageUrl(newUrl);
            }
            return ApiResult.succ(list);
        }
        condition.setOrderHelper(OrderHelper.create("update_date_time", false));
        return bannerService.getPage(condition);
    }

    /**
     * 批量删除（首页图片）
     *
     * @param condition
     * @return
     */
    @DeleteMapping
    public ApiResult deleteMore(@RequestBody List<GeneticBanners> condition) {
        return bannerService.deleteMore(condition);
    }

    /**
     * 批量删除（首页图片）
     *
     * @param
     * @return
     */
    @DeleteMapping("{id}")
    public ApiResult deleteBanner(@PathVariable("id") String id) {
        return bannerService.deleteBanner(id);
    }

}
